package com.zbec.mdrs.license.parser;

import java.io.ByteArrayInputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zbec.framework.core.util.PropertyUtil;
import com.zbec.mdrs.license.security.Crypto;
import com.zbec.mdrs.license.security.MD5Utils;

@Service
public class LicenseParser {
	private static Logger logger = LoggerFactory.getLogger(LicenseParser.class);
	public License getLicense(String licenseMd5) throws Exception {
		logger.info("Start parser: the encode license ="+licenseMd5);
		
		String decpt = Crypto.decrypt(licenseMd5,
				MD5Utils.encodePassword(PropertyUtil.getInstance().getProperty("client.identity")));

		ByteArrayInputStream inStream = new ByteArrayInputStream(
				decpt.getBytes());

		Properties properties = new Properties();
		properties.load(inStream);

		return new License(properties.getProperty("license.expireInDays"),
				properties.getProperty("license.startDate"));
	}
}
